package com.pucheng.bracelet.ui.activity

import android.content.Intent
import android.os.Bundle
import com.aigestudio.wheelpicker.widgets.WheelDatePicker
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.pucheng.bracelet.BuildConfig
import com.pucheng.bracelet.R
import com.pucheng.bracelet.app.base.BaseTitleActivity
import com.pucheng.bracelet.app.ext.KEY_USER_INFO
import com.pucheng.bracelet.app.ext.REQUEST_CODE_TO_USERINFO
import com.pucheng.bracelet.data.bean.UserCenterBean
import com.pucheng.bracelet.databinding.ActivityAboutBinding
import com.pucheng.bracelet.ui.dialog.BirthdaySelectDialog
import com.pucheng.bracelet.ui.dialog.SelectUserInfoDialog
import com.pucheng.bracelet.viewmodel.UserInfoViewModel
import kotlinx.android.synthetic.main.activity_about.*
import kotlinx.android.synthetic.main.activity_user_info.*
import kotlinx.android.synthetic.main.fragment_me.*
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import java.text.FieldPosition
import java.util.*

class UserInfoActivity : BaseTitleActivity<UserInfoViewModel, ActivityAboutBinding>() {
    private lateinit var mAdapter: BaseQuickAdapter<String, BaseViewHolder>
    override fun layoutId(): Int = R.layout.activity_user_info
    override fun initView(savedInstanceState: Bundle?) {
        val strs = resources.getStringArray(R.array.user_info)
        val mData = mutableListOf<String>()
        for (index in strs.indices) {
            mData.add(strs[index])
        }
        mAdapter = object :
            BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_user_info, mData) {
            override fun convert(holder: BaseViewHolder, item: String) {
                holder.setText(R.id.tv_user_info_type, item)
                when (holder.adapterPosition) {
                    0 -> holder.setText(
                        R.id.tv_user_info_content, if (mViewModel.userInfo.value!!.sex == 0) {
                            "女"
                        } else {
                            "男"
                        }
                    )
                    1 -> holder.setText(
                        R.id.tv_user_info_content,
                        "${mViewModel.userInfo.value!!.height}cm"
                    )
                    2 -> holder.setText(
                        R.id.tv_user_info_content,
                        "${mViewModel.userInfo.value!!.weight}kg"
                    )
                    3 -> holder.setText(
                        R.id.tv_user_info_content,
                        mViewModel.userInfo.value!!.birthday
                    )
                    4 -> holder.setText(
                        R.id.tv_user_info_content,
                        "${mViewModel.userInfo.value!!.step}cm"
                    )
                }
            }

        }
        rv_user_info.adapter = mAdapter

        mAdapter.setOnItemClickListener { _, _, position ->
            val data = mutableListOf<String>()
            when (position) {
                0 -> run {
                    data.clear()
                    data.add("女")
                    data.add("男")
                    SelectUserInfoDialog(this, data).setOnItemSelectedListener(object :
                        SelectUserInfoDialog.UserinfoDataSelectedListener {
                        override fun onSelectItem(data: String, position: Int) {
                            mViewModel.userInfo.value!!.sex = position
                            mAdapter.notifyDataSetChanged()
                        }
                    }).show()
                }
                1 -> run {
                    data.clear()
                    for (index in 30..250) {
                        data.add("$index")
                    }
                    SelectUserInfoDialog(this, data).setOnItemSelectedListener(object :
                        SelectUserInfoDialog.UserinfoDataSelectedListener {
                        override fun onSelectItem(data: String, position: Int) {
                            mViewModel.userInfo.value!!.height = data.toInt()
                            mAdapter.notifyDataSetChanged()
                        }
                    }).show()
                }
                2 -> run {
                    data.clear()
                    for (index in 10..200) {
                        data.add("$index")
                    }
                    SelectUserInfoDialog(this, data).setOnItemSelectedListener(object :
                        SelectUserInfoDialog.UserinfoDataSelectedListener {
                        override fun onSelectItem(data: String, position: Int) {
                            mViewModel.userInfo.value!!.weight = data.toInt()
                            mAdapter.notifyDataSetChanged()
                        }
                    }).show()
                }
                4 -> run {
                    data.clear()
                    for (index in 30..155) {
                        data.add("$index")
                    }
                    SelectUserInfoDialog(this, data).setOnItemSelectedListener(object :
                        SelectUserInfoDialog.UserinfoDataSelectedListener {
                        override fun onSelectItem(data: String, position: Int) {
                            mViewModel.userInfo.value!!.step = data
                            mAdapter.notifyDataSetChanged()
                        }
                    }).show()
                }

                else ->
                    BirthdaySelectDialog(this).setOnItemSelectedListener(object :
                        SelectUserInfoDialog.UserinfoDataSelectedListener {
                        override fun onSelectItem(data: String, position: Int) {
                            mViewModel.userInfo.value!!.birthday = data.toString()
                            mAdapter.notifyDataSetChanged()
                        }
                    }).show()

            }

        }
    }

    override fun onPause() {
        super.onPause()
        mViewModel.saveUserInfo()
    }

    override fun titleText(): String = getString(R.string.user_info)
}